package kotan.datastore.api;

import java.io.Serializable;

public class Key implements Serializable, Comparable<Key> {
    private static final long serialVersionUID = 1L;
    String appId;
    String appIdNamespace;
    Key parentKey;
    String kind;
    String name;
    long id;

    public Key(String appId, String appIdNamespace, String kind, String name) {
        this(null, appId, appIdNamespace, kind, -1, name);
    }

    public Key(Key parentKey, String appId, String appIdNamespace, String kind, String name) {
        this(parentKey, appId, appIdNamespace, kind, -1, name);
    }

    public Key(String appId, String appIdNamespace, String kind, long id) {
        this(null, appId, appIdNamespace, kind, id, null);
    }

    public Key(Key parentKey, String appId, String appIdNamespace, String kind, long id) {
        this(parentKey, appId, appIdNamespace, kind, id, null);
    }

    Key() {
    }

    Key(Key parentKey, String appId, String appIdNamespace, String kind, long id, String name) {
        this.parentKey = parentKey;
        this.appId = appId;
        this.appIdNamespace = appIdNamespace;
        this.kind = kind;
        this.id = id;
        this.name = name;
    }

    /**
     * 
     * @return
     */
    public String getKind() {
        return kind;
    }

    public long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    @Override
    public int compareTo(Key o) {
        if (o == null) return 1;
        if (this.kind == null && o.kind == null) return 0;
        if (this.kind == null) return -1;
        int result = kind.compareTo(o.kind);
        if (result != 0) return result;
        result = Long.valueOf(this.id).compareTo(o.id);
        if (result != 0) return result;
        if (this.name == null && o.name == null) return 0;
        if (this.name == null) return -1;
        return this.name.compareTo(o.name);
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((appId == null) ? 0 : appId.hashCode());
        result = prime * result + ((appIdNamespace == null) ? 0 : appIdNamespace.hashCode());
        result = prime * result + (int) (id ^ (id >>> 32));
        result = prime * result + ((kind == null) ? 0 : kind.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((parentKey == null) ? 0 : parentKey.hashCode());
        return result;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        Key other = (Key) obj;
        if (appId == null) {
            if (other.appId != null) return false;
        } else if (!appId.equals(other.appId)) return false;
        if (appIdNamespace == null) {
            if (other.appIdNamespace != null) return false;
        } else if (!appIdNamespace.equals(other.appIdNamespace)) return false;
        if (id != other.id) return false;
        if (kind == null) {
            if (other.kind != null) return false;
        } else if (!kind.equals(other.kind)) return false;
        if (name == null) {
            if (other.name != null) return false;
        } else if (!name.equals(other.name)) return false;
        if (parentKey == null) {
            if (other.parentKey != null) return false;
        } else if (!parentKey.equals(other.parentKey)) return false;
        return true;
    }

    @Override
    public String toString() {
        StringBuilder str = new StringBuilder();
        str.append("Key[");
        str.append("kind=").append(kind);
        str.append(", id=").append(id);
        str.append(", name=").append(name);
        str.append(", parentKey=").append(parentKey);
        str.append(", appId=").append(appId);
        str.append(", appIdNamespace=").append(appIdNamespace);
        str.append("]");
        return str.toString();
    }

}
